package by.gravity.pvh.fragments;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.widget.EditText;
import org.holoeverywhere.widget.Spinner;
import org.holoeverywhere.widget.Toast;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup;
import by.gravity.pvh.R;
import by.gravity.pvh.data.SQLDataManager.TypeOrder;
import by.gravity.pvh.model.CommonModel;
import by.gravity.pvh.model.DoorModel;
import by.gravity.pvh.utils.Configuration;
import by.gravity.pvh.utils.Formula;
import by.gravity.pvh.utils.Formula.TypeDoor;
import by.gravity.pvh.utils.Formula.TypeWindow;

public class DoorFragment extends CommonFragment {

	private EditText width;

	private EditText height;

	private RadioGroup typeDoorGroup;

	private RadioGroup typeOpenWindowGroup;

	private EditText underWindowLength;

	private Spinner underWindowWidth;

	private EditText workPrice;

	private RadioGroup doorType;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);
		init();
	}

	private void init() {

		width = (EditText) getView().findViewById(R.id.doorWidth);
		height = (EditText) getView().findViewById(R.id.doorHeight);
		typeOpenWindowGroup = (RadioGroup) getView().findViewById(R.id.typeOpenWindowGroup);
		typeDoorGroup = (RadioGroup) getView().findViewById(R.id.typeDoorGroup);
		underWindowLength = (EditText) getView().findViewById(R.id.underWindowLength);
		underWindowWidth = (Spinner) getView().findViewById(R.id.underWindowWidth);
		underWindowWidth.setAdapter(new ArrayAdapter<Integer>(getActivity(),
				org.holoeverywhere.R.layout.support_simple_spinner_dropdown_item, Configuration.getUnderwindowwidth()));
		workPrice = (EditText) getView().findViewById(R.id.workPrice);
		doorType = (RadioGroup) getView().findViewById(R.id.doorType);

	}

	@Override
	protected String getFormula() {

		String width = this.width.getText().toString();
		String height = this.height.getText().toString();
		String typeWindowString = getValue(this.typeOpenWindowGroup);
		if (typeWindowString.equals("")) {
			Toast.makeText(getActivity(), R.string.notSelectedTypeWindow, Toast.LENGTH_SHORT).show();
			return null;
		}
		TypeWindow typeWindow = typeWindowString.equals(getString(R.string.oneChamger)) ? TypeWindow.ONE_CHAMGER : TypeWindow.TWO_CHAMGER;
		String typeDoorString = getValue(this.typeDoorGroup);
		if (typeDoorString.equals("")) {
			Toast.makeText(getActivity(), R.string.notSelectedTypeDoor, Toast.LENGTH_SHORT).show();
			return null;
		}
		TypeDoor typeDoor = TypeDoor.WITH_OPEN;
		if (typeDoorString.equals(getString(R.string.doorTypeWithOpen))) {
			typeDoor = TypeDoor.WITH_OPEN;
		} else if (typeDoorString.equals(getString(R.string.doorTypeWithOpenAndWind))) {
			typeDoor = TypeDoor.WITH_OPEN_AND_WIND;
		}
		String underwindowLength = underWindowLength.getText().toString();
		String underwindowWidth = String.valueOf(underWindowWidth.getSelectedItemPosition() + 1);

		if (getIntValue(width) > getIntValue(underwindowLength)) {
			Toast.makeText(getActivity(), R.string.underwindowLengthLess, Toast.LENGTH_SHORT).show();
			return null;
		}

		String workPrice = this.workPrice.getText().toString();

		return Formula.door(width, height, typeWindow, typeDoor, underwindowLength, underwindowWidth, workPrice);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return inflater.inflate(R.layout.f_door, null);
	}

	@Override
	protected CommonModel getModel() {
		DoorModel doorModel = new DoorModel();
		doorModel.setHeight(getValue(height));
		doorModel.setTypeDoorGroup(getValue(this.typeDoorGroup));
		doorModel.setTypeOpenWindowGroup(getValue(typeOpenWindowGroup));
		doorModel.setUnderWindowLength(getValue(underWindowLength));
		doorModel.setUnderWindowWidth(getValue(underWindowWidth));
		doorModel.setWidth(getValue(width));
		doorModel.setWorkPrice(getValue(workPrice));
		doorModel.setDoorType(getValue(doorType));
		return doorModel;
	}

	@Override
	protected void init(CommonModel commonModel) {
		DoorModel model = (DoorModel) commonModel;
		setValue(height, model.getHeight());
		setValue(typeDoorGroup, model.getTypeDoorGroup());
		setValue(typeOpenWindowGroup, model.getTypeOpenWindowGroup());
		setValue(underWindowLength, model.getUnderWindowLength());
		setValue(underWindowWidth, model.getUnderWindowWidth());
		setValue(width, model.getWidth());
		setValue(workPrice, model.getWorkPrice());
		setValue(doorType, model.getDoorType());

	}

	@Override
	protected TypeOrder getTypeOrder() {
		return TypeOrder.DOOR;
	}

	@Override
	protected void clear() {
		setEmptyValue(height);
		setEmptyValue(typeDoorGroup);
		setEmptyValue(typeOpenWindowGroup);
		setEmptyValue(underWindowLength);
		setEmptyValue(underWindowWidth);
		setEmptyValue(width);
		setEmptyValue(workPrice);
		setEmptyValue(doorType);
	}

}
